package com.example.file.exception;


import com.example.file.response.Response;
import com.example.file.response.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.server.MissingRequestValueException;
import org.springframework.web.server.ResponseStatusException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR )
    public Response<?> exception(Exception e, ServerHttpRequest request) {
        log.error("errMsg:{}",e.getMessage(),e);
        return Response.fail(ResultEnum.EXCEPTION.getCode(), e.getMessage(), null);
    }
    @ExceptionHandler(value = ResponseStatusException.class)
    public Response<?> responseStatusException(ResponseStatusException e, ServerHttpRequest request, ServerHttpResponse response) {
        response.setStatusCode(e.getStatusCode());
        log.error("errMsg:{}",e.getMessage(),e);
        return Response.fail(ResultEnum.EXCEPTION.getCode(), e.getMessage(), null);
    }



    @ExceptionHandler(value = MissingRequestValueException.class)
    public Response<?> missingRequestValueException(MissingRequestValueException e, ServerHttpRequest request) {
        log.error("errMsg:{}",e.getMessage(),e);
        return Response.fail(ResultEnum.EXCEPTION.getCode(), e.getMessage(), null);
    }

    @ExceptionHandler(value = ServiceException.class)
    public Response<?> serviceException(ServiceException e) {

        log.error("errMsg:{}",e.getMessage(),e);
        return Response.fail(e.getCode(), e.getMessage(), null);
    }

    @ExceptionHandler(WebExchangeBindException.class)
    public Response<?> handleException(WebExchangeBindException e, ServerHttpRequest request) {

        log.error("errMsg:{}",e.getMessage(),e);
        var bindingResult = e.getBindingResult();
        var message = bindingResult(bindingResult);

        return Response.fail(ResultEnum.EXCEPTION.getCode(), message.substring(0, message.length() - 1), null);
    }

    private StringBuilder bindingResult(BindingResult bindingResult) {

        var fes = bindingResult.getFieldErrors();
        var message = new StringBuilder();
        for (FieldError fieldError : fes) {
            message.append("字段:");
            message.append(fieldError.getField());
            message.append(" 验证错误:");
            message.append(fieldError.getDefaultMessage());
            message.append(",");
        }
        return message;
    }
}
